﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using MShop.Core.Service;
using System.Web.Mvc;

namespace MShop.Core.DataAccess
{
    [Castle.Core.Transient(Lifestyle= Castle.Core.LifestyleType.Transient)]
    public class ItemPropDao : CommonDao<ItemProp>, IItemPropDao
    {

        private readonly IRecursionSort<ItemProp> _RecursionService;
        private readonly IObjectContextProvider _Provider;
        public ItemPropDao(IObjectContextProvider provider, IRecursionSort<ItemProp> recursionservicce)
            :base(provider)
        {
            _RecursionService = recursionservicce;
            _Provider = provider;
        }


        #region IRecursionable<ItemProp> Members

        public IEnumerable<ItemProp> Build()
        {
            IList<ItemProp> list = new List<ItemProp>();
            int rowIndex = 1;
            var enums = this.Find(x => x.ParentId == null);
            _RecursionService.Recursion(GetAll(), ref list, ref rowIndex, null);
            return list;
        }

        public IList<SelectListItem> BuildSelectList(object selectedValue = null, bool includeRoot = false)
        {
            IList<SelectListItem> list = new List<SelectListItem>();

            foreach (var item in Build())
            {
                SelectListItem sitem = new SelectListItem();
                sitem.Value = item.Id.ToString();

                string line = "├";
                for (int i = 0; i < item.Depth; i++)
                {
                    line += "—";
                }
                sitem.Text = string.Concat(line, item.Name);
                if (selectedValue != null)
                {
                    if (selectedValue.ToString() == sitem.Value)
                        sitem.Selected = true;
                }
                list.Add(sitem);
            }
            if (includeRoot)
            {
                list.Insert(0, new SelectListItem() { Text = "   ", Value = "" });
            }
            return list;
        }

        #endregion
    }
}